using UnityEngine;
using System.Collections;
using System.Reflection;

public class csPlayerWeaponMenuCtrl : MonoBehaviour 
{
#region Definition

#endregion
	
#region InputData
	public CWeaponUIInfo[]	WeaponUIInformation;	//Information for weapon UI.
	public bool 			UseDefaultSkin= false;	//Auto-Fit
#endregion
	
#region DataMember
	protected GUIStyleState mFocusedStyle	= new GUIStyleState();		//Style for using weapon.
	protected GUIStyleState	mUnFocusedStyle	= new GUIStyleState();		//Style for un-use weapon.
	protected GUIStyleState mDisableStyle	= new GUIStyleState();		//Style for disable weapon.
	
	protected System.Collections.Generic.List<CWeaponUIInfo> mWeaponUIInfo = new System.Collections.Generic.List<CWeaponUIInfo>();	//UI Info List
	
	protected int			mCurSelectedWeapon	= 0;	//Index for weapons
	
	protected Rect			mBorderRect;				//Border Rect
	protected string		mBorderTitle = "Weapons:";
	protected GUIStyle		mBorderStyle;
#endregion
	
#region MemberFunc
	public bool ChangeWeapon(int Index)
	{
		if(Index >= this.mWeaponUIInfo.Count) {
			Debug.LogError("Invalid Weapon(ID = " + Index.ToString() + ").");
			return false;
		}
		
		if( !this.mWeaponUIInfo[Index].IsEnable ) {
			return false;
		}
			
		this.mCurSelectedWeapon = Index;
		return true;
	}
	
	public void EnableWeapon(int Index, bool Enable)
	{
		if(Index >= this.mWeaponUIInfo.Count) {
			csLogTools.LogError("Invalid Weapon(ID = " + Index.ToString() + ").", this.name, MethodBase.GetCurrentMethod().ReflectedType.Name,
			                    															 MethodBase.GetCurrentMethod().Name);
			return;
		}		
		
		this.mWeaponUIInfo[Index].IsEnable = Enable;
	}
#endregion
	
#region EventFunc
#endregion
	
#region StateFunc
#endregion
	
#region SystemFunc
	// Use this for initialization
	void Start () 
	{
	//Set Focused style
		this.mFocusedStyle.textColor 	= Color.red;
		this.mFocusedStyle.background	= null;
		
	//Set Unfocused style
		this.mUnFocusedStyle.textColor	= Color.white;
		this.mUnFocusedStyle.background	= null;
		
	//Set Disable style
		this.mDisableStyle.textColor	= Color.gray;
		this.mDisableStyle.background	= null;
		
	//Convert User setting Weapon Info to Info List.
		foreach(CWeaponUIInfo info in this.WeaponUIInformation) {
			this.mWeaponUIInfo.Add(info);
		}
	}
	
	// Update is called once per frame
	void OnGUI () 
	{				
	//Reset UI position
	//	Find the max length string
		Vector2 TextWH 	= GUI.skin.GetStyle("Box").CalcSize(new GUIContent(this.mBorderTitle));		
		float MaxLen 	= TextWH.x;
		float MaxHeight	= TextWH.y;
		float CurTop = 0.0f;
		float CurLeft = 0.0f;
		
		foreach(CWeaponUIInfo Info in this.mWeaponUIInfo)
		{
			TextWH	= GUI.skin.GetStyle("Label").CalcSize(new GUIContent(Info.Name));	
			if(TextWH.x > MaxLen)
				MaxLen = TextWH.x;
			if(TextWH.y > MaxHeight)
				MaxHeight = TextWH.y;
		}
			
	//	Border
		CurLeft = Screen.width - MaxLen;
		this.mBorderRect 	= new Rect(CurLeft-MaxLen, CurTop, MaxLen*2, MaxHeight * (this.mWeaponUIInfo.Count + 1));
		if(this.UseDefaultSkin)
			this.mBorderStyle 	= new GUIStyle(GUI.skin.GetStyle("Box"));
		this.mBorderStyle.alignment = TextAnchor.UpperLeft;		
		
		if(this.mBorderStyle == null)
			Debug.LogError("NULL Border Style");
			               
		CurTop += MaxHeight;
		
	//	Weapon
		foreach(CWeaponUIInfo Info in this.mWeaponUIInfo)
		{			
			Info.rect	= new Rect(CurLeft-10, CurTop, MaxLen, MaxHeight);
			CurTop += MaxHeight;			
			if(this.UseDefaultSkin)
				Info.Style	= new GUIStyle(GUI.skin.GetStyle("Label"));
		}				
			
		GUI.Box(this.mBorderRect, this.mBorderTitle, this.mBorderStyle);
		
		for( int i = 0 ; i < this.mWeaponUIInfo.Count ; i++ )
		{
			if( i == this.mCurSelectedWeapon )
			{
				this.mWeaponUIInfo[i].Style.normal = this.mFocusedStyle;
				this.mWeaponUIInfo[i].Style.fontStyle = FontStyle.Bold;
			}else{
				this.mWeaponUIInfo[i].Style.normal = this.mUnFocusedStyle;				
				this.mWeaponUIInfo[i].Style.fontStyle = FontStyle.Normal;				
			}
			GUI.Label(this.mWeaponUIInfo[i].rect, this.mWeaponUIInfo[i].Name, this.mWeaponUIInfo[i].Style);
		}				
	}
#endregion	
	
#region PropertiesOP
	public void AddWeaponUIInfo(string WeaponName, Rect rect)
	{
		CWeaponUIInfo info 	= new CWeaponUIInfo();
		info.Name 			= WeaponName;
		
		if(!this.UseDefaultSkin)
		{
			info.rect = rect;
		}else{
		}
		
		info.Name = (this.mWeaponUIInfo.Count + 1).ToString() + "." + info.Name;
		this.mWeaponUIInfo.Add(info);
	}
	
	protected void SetUseDefaultSkin(bool IsUsingDefault) {
		this.UseDefaultSkin = IsUsingDefault;
	}
#endregion
}

[System.Serializable]
public class CWeaponUIInfo
{
	public string 	Name	= "WeaponName";
	public Rect	  	rect	= new Rect(0.0f, 0.0f, 0.0f, 0.0f);
	public GUIStyle	Style	= null;
	public bool		IsEnable= false;
	
}